package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.content_category.TbContentCategory;
import com.dongdongshop.service.content_category.ContentCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("category")
public class ContentCategoryController {

    private final Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference
    private ContentCategoryService categoryService;

    @RequestMapping("toCategory")
    public String toCategory(){
        return "admin/content_category";
    }

    @RequestMapping("listCategory")
    @ResponseBody
    public Result listCategory(String name){
        try{
           List<TbContentCategory> listCategorys = categoryService.listCategory(name);
           return Result.ok().setData(listCategorys);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.fail();
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbContentCategory category){
        System.out.println("category"+category);
        try{
            int count = categoryService.insert(category);
            if(count >= 1){
                return Result.ok();
            }else{
                return Result.fail();
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.fail();
        }
    }

    @RequestMapping("getCategoryById")
    @ResponseBody
    public Result getCategoryById(Long id){
        try{
            TbContentCategory category = categoryService.getCategoryById(id);
            return Result.ok().setData(category);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.fail();
        }
    }


    @RequestMapping("updateCategoryById")
    @ResponseBody
    public Result updateCategoryById(TbContentCategory category){
        try{
            int count = categoryService.updateCategory(category);
            if(count >= 1){
                return Result.ok();
            }else{
                return Result.fail();
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.fail();
        }
    }

    @RequestMapping("deleteCategory")
    @ResponseBody
    public Result deleteCategory(@RequestParam("ids[]")Integer[] ids){
        try{
            int count = categoryService.deleteCategory(ids);
            if(count >= 1){
                return Result.ok();
            }else{
                return Result.fail();
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.fail();
        }
    }

}
